#include "Player.h"
#include "ENUM Pozycje.h"


Player::Player(void):
	playerSpeed(150)
{
	PlayerShape.setSize(sf::Vector2f(35,35));
	PlayerShape.setOrigin(PlayerShape.getSize().x/2, PlayerShape.getSize().y/2);
	PlayerShape.setPosition(Przypisz_Pozycje_na_mape(&PlayerShape, Poz_5, Poz_13));

	mPlayerTexture.loadFromFile("Grafiki/czolg.png");
	
	PlayerShape.setTexture(&mPlayerTexture);
	mPlayerTexture.setSmooth(true);

	moveLeft = moveRight = moveUp = moveDown = bShoot = false;

	PlayerRotacion = Up;
}

void Player::inicializePlanszaColision(sf::RectangleShape* Plansza)
{
	mColision.updatePlansza(Plansza);
}


void Player::update(sf::Time elapsedTime)
{
	mColision.updatePlayer(&PlayerShape);
	mShooter.update(elapsedTime);
	mColision.updateColisions();

		if(moveRight)
		{
			PlayerRotacion = Right;

			if(!mColision.ColsioionRight)
			PlayerShape.move(playerSpeed * elapsedTime.asSeconds(), 0 );
		}
	else
		if(moveLeft)
		{
			PlayerRotacion = Left;

			if(!mColision.ColisionLeft)
			PlayerShape.move(-(playerSpeed * elapsedTime.asSeconds()), 0 );
		}
	else
		if(moveUp)
		{
			PlayerRotacion = Up;

			if(!mColision.ColisionUp)
			PlayerShape.move(0, -(playerSpeed * elapsedTime.asSeconds()) );
		}
	else
		if(moveDown)
		{
			PlayerRotacion = Down;

			if(!mColision.ColisionDown)
			PlayerShape.move(0, playerSpeed * elapsedTime.asSeconds() );
		}

		PlayerShape.setRotation(PlayerRotacion);

		if(bShoot)
			mShooter.Attack(PlayerRotacion, PlayerShape.getPosition());
		
}

void Player::draw(sf::RenderWindow* Window)
{
	mWindow = Window;
	mWindow->draw(PlayerShape);

	mShooter.draw(mWindow);
}



void Player::KeyPressed(sf::Event::KeyEvent key)
{
	if(key.code == sf::Keyboard::D)
		moveRight = true;
	if(key.code == sf::Keyboard::A)
		moveLeft = true;
	if(key.code == sf::Keyboard::S)
		moveDown = true;
	if(key.code == sf::Keyboard::W)
		moveUp = true;
	if(key.code == sf::Keyboard::Space)
		bShoot = true;
}

void Player::KeyReleased(sf::Event::KeyEvent key)
{
	if(key.code == sf::Keyboard::D)
		moveRight = false;
	if(key.code == sf::Keyboard::A)
		moveLeft = false;
	if(key.code == sf::Keyboard::S)
		moveDown = false;
	if(key.code == sf::Keyboard::W)
		moveUp = false;
	if(key.code == sf::Keyboard::Space)
		bShoot = false;
}